package com.ms.dsq_english.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;

public class FileSizeUtils
{
    //获取文件大小单位为B的double值
    public static final int SIZETYPE_B = 1;
    //获取文件大小单位为KB的double值
    public static final int SIZETYPE_KB = 2;
    //获取文件大小单位为MB的double值
    public static final int SIZETYPE_MB = 3;
    //获取文件大小单位为GB的double值
    public static final int SIZETYPE_GB = 4;

    //获取文件指定文件的指定单位的大小,获取大小的类型1为B、2为KB、3为MB、4为GB
    public static String getFileSizeStrByType(File file, int type)
    {
        int size = 0;
        try
        {
            if (file.isDirectory())
            {
                size = getFilesSize(file);
            }
            else
            {
                size = getFileSize(file);
            }
        }
        catch (Exception e) {e.printStackTrace();}

        return formatFileSizeByType(size, type);
    }

    //计算指定文件或指定文件夹的大小
    public static String getFileSizeStr(File file)
    {
        int size = 0;
        try
        {
            if (file.isDirectory())
            {
                size = getFilesSize(file);
            }
            else
            {
                size = getFileSize(file);
            }
        }
        catch (Exception e) {e.printStackTrace();}

        return formatFileSize(size);
    }

    //获取指定文件大小
    public static int getFileSize(File file)
    {
        int size = 0;
        try
        {
            if (file.exists())
            {
                FileInputStream fis = null;
                fis = new FileInputStream(file);
                size = fis.available();
            }
            else
            {
                file.createNewFile();
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return size;
    }

    //获取指定文件夹包含内容大小
    public static int getFilesSize(File files)
    {
        int size = 0;

        File file_a[] = files.listFiles();
        for (File file : file_a)
        {
            if (file.isDirectory())
            {
                size = size + getFilesSize(file);
            }
            else
            {
                size = size + getFileSize(file);
            }
        }

        return size;
    }

    //转换文件大小
    public static String formatFileSize(int fileSize)
    {
        DecimalFormat df = new DecimalFormat("#.00");

        String fileSizeString;

        if (fileSize == 0)
        {
            return "0KB";
        }
        if (fileSize < 1024)
        {
            fileSizeString = df.format((double) fileSize) + "B";
        }
        else if (fileSize < 1048576)
        {
            fileSizeString = df.format((double) fileSize / 1024) + "KB";
        }
        else if (fileSize < 1073741824)
        {
            fileSizeString = df.format((double) fileSize / 1048576) + "MB";
        }
        else
        {
            fileSizeString = df.format((double) fileSize / 1073741824) + "GB";
        }

        return fileSizeString;
    }

    public static String formatFileSizeKb(int fileSize)
    {
        DecimalFormat df = new DecimalFormat("#.00");

        String fileSizeString;

        if (fileSize == 0)
        {
            return "0KB";
        }
        if (fileSize < 1024)
        {
            fileSizeString = df.format((double) fileSize) + "B";
        }
        else if (fileSize < 1048576)
        {
            fileSizeString = df.format((double) fileSize / 1024) + "KB";
        }
        else if (fileSize < 1073741824)
        {
            fileSizeString = df.format((double) fileSize / 1048576) + "MB";
        }
        else
        {
            fileSizeString = df.format((double) fileSize / 1073741824) + "GB";
        }

        return fileSizeString;
    }




    //转换文件大小,指定转换的类型
    public static String formatFileSizeByType(int fileSize, int type)
    {
        DecimalFormat df = new DecimalFormat("#.00");

        String fileSizeString = "";

        switch (type)
        {
            case SIZETYPE_B:
            {
                fileSizeString = df.format((double) fileSize) + "B";
                break;
            }
            case SIZETYPE_KB:
            {
                fileSizeString = df.format((double) fileSize / 1024) + "KB";
                break;
            }
            case SIZETYPE_MB:
            {
                fileSizeString = df.format((double) fileSize / 1048576) + "MB";
                break;
            }
            case SIZETYPE_GB:
            {
                fileSizeString = df.format((double) fileSize / 1073741824) + "GB";
                break;
            }
            default:
            {
                break;
            }
        }

        return fileSizeString;
    }
}
